package com.project.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.MyBatisConfig;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional // 事务管理
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private IStudentMapper mapper;
    @Override
    public void add(StudentBean student) {
        mapper.add(student);
    }

    @Override
    public void del(int id) {
        mapper.del(id);
    }

    @Override
    public void update(int id, String tel) {
        mapper.update(id, tel);
    }

    @Override
    public List<StudentBean> findAll() {
        return mapper.findAll();
    }

    @Override
    public PageInfo<StudentBean> cutAll(int pageNO) {
        PageHelper.startPage(pageNO, 3);
        PageInfo<StudentBean> pageInfo =  PageInfo.of(mapper.findAll());

        return pageInfo;
    }

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyBatisConfig.class);
        IStudentService service = context.getBean(IStudentService.class);
        service.add(new StudentBean("123", "13203928732", LocalDate.parse("1992-09-12")));
    }
}
